//
//  NewsViewController.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "NewsViewController.h"
#import "RequestURl.h"
#import "CommonModel.h"
#import "PicModel.h"
#import "CommonCell.h"
#import "PicCell.h"
#import <UIImageView+WebCache.h>


@interface NewsViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)NSMutableArray *arrayAllData;
@property(nonatomic, strong)NSMutableArray *arrayPicData;
@end

@implementation NewsViewController


- (void)loadView {
    
    
    self.sortArray = [NSMutableArray arrayWithObjects:@"头条",@"苹果",@"新闻",@"手机",@"评测",@"视频",@"企业",@"游戏", nil];
    self.buttonWidth = 48;
    self.newsV = [[NewsView alloc] initWithFrame:[UIScreen mainScreen].bounds sortArray:self.sortArray buttonWidth:self.buttonWidth] ;
    
    self.view = self.newsV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    self.newsV.sortSV.delegate = self;
    self.newsV.showView.delegate = self;
    
    NSInteger i = 110;
    for (UIButton *button in self.newsV.sortSV.subviews) {
        if ([button isKindOfClass:[UIButton class]] == YES) {
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            button.tag = i++;
        }
        if (i == 110) {
            [self buttonAction:button];
        }
    }
    
    
    for (UITableView *tableView in self.newsV.showView.subviews) {
        if ([tableView isKindOfClass:[UITableView class]] == YES) {
            tableView.delegate = self;
            tableView.dataSource = self;
            [tableView registerNib:[UINib nibWithNibName:@"CommonCell" bundle:nil] forCellReuseIdentifier:@"common_id"];
            [tableView registerNib:[UINib nibWithNibName:@"PicCell" bundle:nil] forCellReuseIdentifier:@"pic_id"];
            tableView.rowHeight = UITableViewAutomaticDimension;
            tableView.estimatedRowHeight = 2;
        }
    }
    
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
//    self.tableView.estimatedRowHeight = 2;
    
    RequestURl *request = [[RequestURl alloc] init];
    [request requestWithUrlID:0 UrlPage:1 success:^(id item0, id item1) {
        self.arrayAllData = item0;
        self.arrayPicData = item1;
        NSLog(@"%@",self.arrayAllData);
        NSInteger offset = self.newsV.showView.contentOffset.x / kScreenW;
        if ([self.newsV.showView.subviews[offset] isKindOfClass:[UITableView class]]) {
            [(UITableView *)self.newsV.showView.subviews[offset] reloadData];
        }
        
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}




//tableview代理
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayAllData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id baseM = self.arrayAllData[indexPath.row];
//    self.arrayAllData[indexPath.row]
    NSString *typeStr = [baseM valueForKey:@"type"];
    if ([typeStr isEqualToString:@"6"] == NO) {
        CommonCell *cell = [tableView dequeueReusableCellWithIdentifier:@"common_id" forIndexPath:indexPath];
        CommonModel *commonM = self.arrayAllData[indexPath.row];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:commonM.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholder1.jpg"]];
        cell.title.text = commonM.stitle;
        cell.time.text = [self handleDateWithDate:commonM.sdate];
        cell.commen_num.text = [NSString stringWithFormat:@"%ld",commonM.comment_num];
        return cell;
    } else {
        PicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pic_id" forIndexPath:indexPath];
        PicModel *picM = self.arrayAllData[indexPath.row];
        cell.title.text = picM.stitle;
//        cell.
        [cell.firstImgV sd_setImageWithURL:[NSURL URLWithString:picM.picArray[0]] placeholderImage:[UIImage imageNamed:@"placeholder2.jpg"]];
        [cell.secondImgV sd_setImageWithURL:[NSURL URLWithString:picM.picArray[1]] placeholderImage:[UIImage imageNamed:@"placeholder2.jpg"]];
        [cell.thirdImgV sd_setImageWithURL:[NSURL URLWithString:picM.picArray[2]] placeholderImage:[UIImage imageNamed:@"placeholder2.jpg"]];
        return cell;
    }
    
}



#pragma mark 截取日期
- (NSString *)handleDateWithDate:(NSString *)date {
    
    NSString *month = [date substringWithRange:NSMakeRange(5, 2)];
    NSString *day = [date substringWithRange:NSMakeRange(8, 2)];
    
    NSString *firstMonth = [month substringToIndex:0];
    NSString *firstDay = [day substringToIndex:0];
    
    if ([firstMonth isEqualToString:@"0"] == YES) {
        month = [month substringFromIndex:1];
    }
    if ([firstDay isEqualToString:@"0"] == YES) {
        day = [day substringFromIndex:1];
    }
    
    return [NSString stringWithFormat:@"%@-%@",month,day];
}


//分类切换按钮响应事件
- (void)buttonAction:(UIButton *)sender {
    NSInteger offset = sender.tag - 110;
    
    [UIView animateWithDuration:1 animations:^{
        if (offset < self.sortArray.count - 2 && offset > 2) {
            self.newsV.sortSV.contentOffset = CGPointMake((offset - 3) * self.buttonWidth, 0);
        } else if(offset < 3) {
            self.newsV.sortSV.contentOffset = CGPointMake(0, 0);
        } else {
            self.newsV.sortSV.contentOffset = CGPointMake((self.sortArray.count - offset) * self.buttonWidth, 0);
        }
        
        self.newsV.showView.contentOffset = CGPointMake(offset * kScreenW, 0);
    }];
    
}

//scrollView代理
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView.tag == 101) {
        return;
    }
    
    CGFloat offset = self.newsV.showView.contentOffset.x;
    CGFloat scale = offset / kScreenW;
    offset = self.buttonWidth * scale;
    self.newsV.sortSV.contentOffset = CGPointMake(offset, 0);
    
}

- (NSMutableArray *)arrayAllData {
    if (!_arrayAllData) {
        _arrayAllData = [NSMutableArray array];
    }
    return _arrayAllData;
}

- (NSMutableArray *)arrayPicData {
    if (!_arrayPicData) {
        _arrayPicData = [NSMutableArray array];
    }
    return _arrayPicData;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
